package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class ActivityBasicRegisterUsername extends ActivityBasic {

	private final String TAG = "ActivityBasicRegisterUsername";

	private String str_requestName = "newReg.php";

	private String str_uname;

	private String str_pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void requestNetworkData(Object... args) {

		str_uname = args[0].toString();

		str_pwd = args[1].toString();

		String str_requestURL = str_server + str_requestName;

		AsyncHttpClient client = new AsyncHttpClient();

		RequestParams params = new RequestParams();

		params.put("body", getJsonArgs());

		client.post(str_requestURL, params, responseHandler);
	}

	private String getJsonArgs() {

		Map<String, Object> body = new HashMap<String, Object>();
		body.put("uname", str_uname);
		body.put("pwd", str_pwd);

		Gson gson = new Gson();
		String s = gson.toJson(body);

		return s;

	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			try {

				String str_jsonReturn = new String(responseBody, "UTF-8");

				Log.i(TAG, str_jsonReturn);

				JsonParser parser = new JsonParser();

				JsonObject jsObj = parser.parse(str_jsonReturn)
						.getAsJsonObject();

				requestFinish(jsObj);

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			// TODO 自动生成的方法存根

		}
	};
}
